package com.hnevc.xiaoming.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.hnevc.xiaoming.R;
import com.hnevc.xiaoming.adapter.ViewPagerAdapter;
import com.hnevc.xiaoming.fragment.FragmentDynamic;
import com.hnevc.xiaoming.fragment.FragmentHomePage;
import com.hnevc.xiaoming.fragment.FragmentMine;

import java.util.ArrayList;
import java.util.List;

public class MyAppActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager my_app_vp;
    private Button ig_btn_1;
    private Button ig_btn_2;
    private Button ig_btn_3;
    private ArrayList<Fragment> fragments;
    private List<View> dots;
    private int oldPosition = 0;// 记录上一次点的位置

    private int currentItem; // 当前页面
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_vf_app_layout);
        initView();

    }

    private void initView() {
        my_app_vp = (ViewPager) findViewById(R.id.my_app_vp);

        ig_btn_1 = (Button) findViewById(R.id.ig_btn_1);
        ig_btn_2 = (Button) findViewById(R.id.ig_btn_2);
        ig_btn_3 = (Button) findViewById(R.id.ig_btn_3);

        ig_btn_1.setOnClickListener(this);
        ig_btn_2.setOnClickListener(this);
        ig_btn_3.setOnClickListener(this);

        setView();
    }

    private void setView() {

        fragments = new ArrayList<>();
        fragments.add(new FragmentHomePage());
        fragments.add(new FragmentMine());
        fragments.add(new FragmentDynamic());
        my_app_vp.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),fragments));


        dots = new ArrayList<>();
        dots.add(ig_btn_1);
        dots.add(ig_btn_2);
        dots.add(ig_btn_3);

        dots.get(0).setBackgroundResource(R.drawable.xiaoyuandian_null);
        my_app_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {

                //获取上一个位置，并且把点的状体设置成默认状体
                dots.get(oldPosition).setBackgroundResource(R.drawable.xiaoyuandian);
                //获取到选中页面对应的点，设置为选中状态
                dots.get(position).setBackgroundResource(R.drawable.xiaoyuandian_null);
                //下面是记录本次的位置，因为在滑动，他就会变成过时的点了
                oldPosition = position;
                //关联页卡
                currentItem = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ig_btn_1:

                break;
            case R.id.ig_btn_2:

                break;
            case R.id.ig_btn_3:

                break;
        }
    }


}